

package com.yami.shop.sys.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.sys.model.SysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.yami.shop.common.util.PageParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.sys.service.SysConfigService;
import com.yami.shop.common.annotation.SysLog;

import cn.hutool.core.util.StrUtil;



/**
 * 系统配置信息
 *
 */
@RestController
@Tag(name = "系统配置信息接口")
@RequestMapping("/sys/config")
public class SysConfigController{
	@Autowired
	private SysConfigService sysConfigService;

	/**
	 * 所有配置列表
	 */
	@GetMapping("/page")
	@Operation(summary = "所有配置列表")
	@PreAuthorize("@pms.hasPermission('sys:config:page')")//权限管理
	public ServerResponseEntity<IPage<SysConfig>> page(String paramKey,PageParam<SysConfig> page){
		IPage<SysConfig> sysConfigs = sysConfigService.page(page, new LambdaQueryWrapper<SysConfig>().like(StrUtil.isNotBlank(paramKey),SysConfig::getParamKey,paramKey));
		return ServerResponseEntity.success(sysConfigs);
	}


	/**
	 * 根据id配置信息
	 */
	@GetMapping("/info/{id}")
	@Operation(summary = "根据id配置信息")
	@PreAuthorize("@pms.hasPermission('sys:config:info')")//权限管理
	public ServerResponseEntity<SysConfig> info(@PathVariable("id") Long id){
		SysConfig config = sysConfigService.getById(id);
		return ServerResponseEntity.success(config);
	}

	/**
	 * 保存配置
	 */
	@SysLog("保存配置") //日志输出
	@PostMapping
	@Operation(summary = "保存配置")
	@PreAuthorize("@pms.hasPermission('sys:config:save')")//权限管理
	public ServerResponseEntity<Void> save(@RequestBody @Valid SysConfig config){
		sysConfigService.save(config);
		return ServerResponseEntity.success();
	}

	/**
	 * 修改配置
	 */
	@SysLog("修改配置") //日志输出
	@PutMapping
	@Operation(summary = "修改配置")
	@PreAuthorize("@pms.hasPermission('sys:config:update')")//权限管理
	public ServerResponseEntity<Void> update(@RequestBody @Valid SysConfig config){
		sysConfigService.updateById(config);
		return ServerResponseEntity.success();
	}

	/**
	 * 删除配置
	 */
	@SysLog("删除配置") //日志输出
	@DeleteMapping
	@Operation(summary = "删除配置")
	@PreAuthorize("@pms.hasPermission('sys:config:delete')")//权限管理
	public ServerResponseEntity<Void> delete(@RequestBody Long[] configIds){
		sysConfigService.deleteBatch(configIds);
		return ServerResponseEntity.success();
	}

}
